package utils

import (
	"encoding/json"
	"fmt"
	"strconv"
)

type StringArrayInt64 []int64

func (s *StringArrayInt64) UnmarshalJSON(value []byte) error {
	var v []string
	err := json.Unmarshal(value, &v)
	if err != nil {
		return err
	}
	for i := range v {
		val, err := strconv.ParseInt(v[i], 10, 64)
		if err != nil {
			return err
		}
		*s = append(*s, val)
	}
	return nil
}

func (s StringArrayInt64) MarshalJSON() ([]byte, error) {
	var v = make([]string, len(s))
	for i, j := range s {
		v[i] = strconv.FormatInt(j, 10)
	}
	return json.Marshal(v)
}

func (s StringArrayInt64) RestoreType() []int64 {
	var r = make([]int64, len(s))
	for i, v := range s {
		r[i] = v
	}
	return r
}

// Int64ArrToStringArray 将 []int64 转换为 []string
func Int64ArrToStringArray(li []int64) []string {
	arr := make([]string, len(li))
	for _, v := range li {
		arr = append(arr, fmt.Sprintf("%d", v))
	}
	return arr
}
